package wk.asm.test;

import jdk.internal.org.objectweb.asm.ClassVisitor;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import jdk.internal.org.objectweb.asm.Opcodes;
import wk.asm.test.visitor.MyMethodVisitor;

/**
 * Created by wangkuan at 2018-09-05 16:29
 */
public class MySubClassVisitor extends ClassVisitor {
    private String enhancedSuperName;

    public MySubClassVisitor(int version, ClassVisitor classVisitor) {
        super(version, classVisitor);
    }

    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        String enhancedName = name + "$EnhancedByASM"; //改变类命名
        this.enhancedSuperName = name;

        super.visit(version, access, enhancedName, signature, enhancedSuperName, interfaces);
    }

    @Override
    public MethodVisitor visitMethod(int version, String name, String desc, String signature, String[] strings) {
        version = Opcodes.ASM5;

        MethodVisitor mv = cv.visitMethod(version, name, desc, signature, strings);

        if (mv != null) {
            if ("run".equals(name)) {
                return new MyMethodVisitor(version, mv);
            }
            if ("<init>".equals(name)) {
                return new MySubMethodVisitor(version, mv, enhancedSuperName);
            }
        }

        return mv;
    }
}
